//
//  SettingsController.swift
//  CarShare
//
//  Created by Pro on 2017/12/26.
//

import UIKit

private let settingCellID = "settingCell"

class SettingsController: BaseController {
    
    private lazy var setViewModel = SettingViewModel()
    var titleArr = [["用户协议","押金说明","充值说明"],["清除缓存","当前版本","关于我们"]]
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBar.isHidden = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "设置"
        creatTableViewFooter()
    }
    // MARK: - 退出登录
    
    @objc private func didClickSignOutButton(sender: UIButton) {
        
        keyWindow?.rootViewController = BaseNavigationController(rootViewController: LoginController())
        
        setViewModel.requestSignout { (isSuccess, msg) in
            
            if isSuccess {
                
                keyWindow?.rootViewController = BaseNavigationController(rootViewController: LoginController())
                self.showError(msg)
                
            } else {
                
            }
        }
    }
    private lazy  var tableview : UITableView = {
        
        let tableview = UITableView(frame:self.view.bounds, style: .grouped)
        tableview.delegate = self
        tableview.dataSource = self
        
        tableview.rowHeight = 50
        tableview.isScrollEnabled = false
        tableview.separatorStyle = .none // 分隔线
        
        tableview.register(UINib.init(nibName: "SettingCell", bundle: Bundle.main), forCellReuseIdentifier: settingCellID)
        
        if #available(iOS 11.0, *){
            tableview.estimatedSectionHeaderHeight = 8
            tableview.estimatedSectionFooterHeight = 0.01
        }
        return tableview
    }()
    private lazy var exitBtn: UIButton = UIButton(es_title: "退出登录", titleColor: UIColor.white, backColor: APPColor, font: 15, target: self, action: #selector(didClickSignOutButton))
}

extension SettingsController {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return titleArr.count
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        let sectionArr = titleArr[section]
        return sectionArr.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let title = titleArr[indexPath.section][indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: settingCellID, for: indexPath) as? SettingCell
        
        cell?.titleLab.text = title

        if indexPath.section == 1 {
            
            if indexPath.row == 0 {
              
                cell?.subtitleContant.constant = 12
                cell?.subtitleLab.text = "3M"
                cell?.arrowIcon.isHidden = true
            }
            
            if indexPath.row == 1 {
                // 版本号
                let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
                let versionStr = "v\(String(describing: currentVersion))"
                
                cell?.subtitleContant.constant = 12
                cell?.subtitleLab.text = versionStr
                cell?.arrowIcon.isHidden = true
                
            }
        }
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        
        
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 8
    }
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: screenBounds.width, height: 8))
        view.backgroundColor = ColorAppBack
        return view
    }
}
// MARK: - Setup UI

extension SettingsController {
    
    override func setupUI() {
        super.setupUI()
        
        view.addSubview(tableview)
    }
    func creatTableViewFooter() {
        tableview.tableFooterView = nil
        
        let footView = UIView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 50))
        footView.backgroundColor = UIColor.clear
        exitBtn.frame = CGRect(x: 10, y: 0, width: screenBounds.width - 20, height: 50)
        footView.addSubview(exitBtn)
        tableview.tableFooterView = footView
        
        
    }
}
